﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace DALL.SysManage
{
    public class T_PUB_HOTEL
    {
        public T_PUB_HOTEL()
        { }

        /// <summary>
        /// 智能提示酒店名称
        /// </summary>
        /// <returns></returns>
        public DataSet GetHotelName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT HOTEL_NAME FROM T_PUB_HOTEL ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SysManage.T_PUB_HOTEL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_HOTEL(");
            strSql.Append("HOTEL_ID,HOTEL_NAME,BRANCH_ID,DESCRIPTION,HOTEL_DESK_NUM,HOTEL_STYLE,HOTEL_SPECIFICATION,HOTEL_LOCATION)");

            strSql.Append(" values (");
            strSql.Append("@HOTEL_ID,@HOTEL_NAME,@BRANCH_ID,@DESCRIPTION,@HOTEL_DESK_NUM,@HOTEL_STYLE,@HOTEL_SPECIFICATION,@HOTEL_LOCATION)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HOTEL_ID", DbType.String, model.HOTEL_ID);
            db.AddInParameter(dbCommand, "HOTEL_NAME", DbType.String, model.HOTEL_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "DESCRIPTION", DbType.String, model.DESCRIPTION);
            db.AddInParameter(dbCommand, "HOTEL_DESK_NUM", DbType.String, model.HOTEL_DESK_NUM);
            db.AddInParameter(dbCommand, "HOTEL_STYLE", DbType.String, model.HOTEL_STYLE);
            db.AddInParameter(dbCommand, "HOTEL_SPECIFICATION", DbType.String, model.HOTEL_SPECIFICATION);
            db.AddInParameter(dbCommand, "HOTEL_LOCATION", DbType.String, model.HOTEL_LOCATION);
            int result;
            object obj = db.ExecuteNonQuery(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_HOTEL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_HOTEL set ");
            strSql.Append("HOTEL_NAME=@HOTEL_NAME,");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("DESCRIPTION=@DESCRIPTION,");
            strSql.Append("HOTEL_DESK_NUM=@HOTEL_DESK_NUM,");
            strSql.Append("HOTEL_STYLE=@HOTEL_STYLE,");
            strSql.Append("HOTEL_SPECIFICATION=@HOTEL_SPECIFICATION,");
            strSql.Append("HOTEL_LOCATION=@HOTEL_LOCATION");
            strSql.Append(" where HOTEL_ID=@HOTEL_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HOTEL_ID", DbType.String, model.HOTEL_ID);
            db.AddInParameter(dbCommand, "HOTEL_NAME", DbType.String, model.HOTEL_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "DESCRIPTION", DbType.String, model.DESCRIPTION);
            db.AddInParameter(dbCommand, "HOTEL_DESK_NUM", DbType.String, model.HOTEL_DESK_NUM);
            db.AddInParameter(dbCommand, "HOTEL_STYLE", DbType.String, model.HOTEL_STYLE);
            db.AddInParameter(dbCommand, "HOTEL_SPECIFICATION", DbType.String, model.HOTEL_SPECIFICATION);
            db.AddInParameter(dbCommand, "HOTEL_LOCATION", DbType.String, model.HOTEL_LOCATION);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string HOTEL_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_HOTEL ");
            strSql.Append(" where HOTEL_ID=@HOTEL_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HOTEL_ID", DbType.String, HOTEL_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_HOTEL GetModel(string HOTEL_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HOTEL_ID,HOTEL_NAME,BRANCH_ID,DESCRIPTION,HOTEL_DESK_NUM,HOTEL_STYLE,HOTEL_SPECIFICATION,HOTEL_LOCATION from T_PUB_HOTEL ");
            strSql.Append(" where HOTEL_ID=@HOTEL_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HOTEL_ID", DbType.String, HOTEL_ID);
            Model.SysManage.T_PUB_HOTEL model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HOTEL_ID,HOTEL_NAME,BRANCH_ID,DESCRIPTION,HOTEL_DESK_NUM,HOTEL_STYLE,HOTEL_SPECIFICATION,HOTEL_LOCATION ");
            strSql.Append(" FROM T_PUB_HOTEL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_HOTEL> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HOTEL_ID,HOTEL_NAME,BRANCH_ID,DESCRIPTION,HOTEL_DESK_NUM,HOTEL_STYLE,HOTEL_SPECIFICATION,HOTEL_LOCATION ");
            strSql.Append(" FROM T_PUB_HOTEL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_HOTEL> list = new List<Model.SysManage.T_PUB_HOTEL>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_HOTEL ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_HOTEL model = new Model.SysManage.T_PUB_HOTEL();
            object ojb;
            model.HOTEL_ID = dataReader["HOTEL_ID"].ToString();
            model.HOTEL_NAME = dataReader["HOTEL_NAME"].ToString();
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            model.DESCRIPTION = dataReader["DESCRIPTION"].ToString();
            model.HOTEL_DESK_NUM = dataReader["HOTEL_DESK_NUM"].ToString();
            model.HOTEL_STYLE = dataReader["HOTEL_STYLE"].ToString();
            model.HOTEL_SPECIFICATION = dataReader["HOTEL_SPECIFICATION"].ToString();
            model.HOTEL_LOCATION = dataReader["HOTEL_LOCATION"].ToString();
            return model;
        }

        #endregion  Method
    }
}
